package com.dianping.cat.home.dependency.graph.transform;

import com.dianping.cat.home.dependency.graph.IEntity;
import com.dianping.cat.home.dependency.graph.entity.TopologyEdge;
import com.dianping.cat.home.dependency.graph.entity.TopologyGraph;
import com.dianping.cat.home.dependency.graph.entity.TopologyNode;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;









public class DefaultJsonParser<T>
{
  private DefaultLinker m_linker;
  private Stack<String> m_tags;
  private Stack<Object> m_objs;
  private List<T> m_entities;
  private Class<?> m_entityClass;

  private DefaultJsonParser(Class<?> entityClass) {
/*  34 */     this.m_linker = new DefaultLinker(true);

    this.m_tags = new Stack();

/*  38 */     this.m_objs = new Stack();

/*  40 */     this.m_entities = new ArrayList();




/*  45 */     this.m_entityClass = entityClass;
  }


/*  49 */   public static <T extends IEntity<T>> T parse(Class<T> entityClass, InputStream in) throws IOException { return (T)parse(entityClass, new InputStreamReader(in, "utf-8")); }



  public static <T extends IEntity<T>> T parse(Class<T> entityClass, Reader reader) throws IOException {
/*  54 */     DefaultJsonParser parser = new DefaultJsonParser(entityClass);

/*  56 */     parser.onArrayBegin();
/*  57 */     parser.parse(new JsonReader(reader));
/*  58 */     parser.onArrayEnd();

/*  60 */     if (parser.m_entities.isEmpty()) {
/*  61 */       return null;
    }
/*  63 */     return (T)(IEntity)parser.m_entities.get(0);
  }



/*  68 */   public static <T extends IEntity<T>> T parse(Class<T> entityClass, String json) throws IOException { return (T)parse(entityClass, new StringReader(json)); }



/*  72 */   public static <T extends IEntity<T>> List<T> parseArray(Class<T> entityClass, InputStream in) throws Exception { return parseArray(entityClass, new InputStreamReader(in, "utf-8")); }



  public static <T extends IEntity<T>> List<T> parseArray(Class<T> entityClass, Reader reader) throws Exception {
/*  77 */     DefaultJsonParser parser = new DefaultJsonParser(entityClass);

/*  79 */     parser.parse(new JsonReader(reader));
/*  80 */     return (List<T>) parser.m_entities;
  }


/*  84 */   public static <T extends IEntity<T>> List<T> parseArray(Class<T> entityClass, String json) throws Exception { return parseArray(entityClass, new StringReader(json)); }




  protected <T> T convert(Class<T> type, String value, T defaultValue) {
/*  90 */     if (value == null || value.length() == 0) {
/*  91 */       return defaultValue;
    }

/*  94 */     if (type == Boolean.class)
      return (T)Boolean.valueOf(value);
/*  96 */     if (type == Integer.class)
/*  97 */       return (T)Integer.valueOf(value); 
/*  98 */     if (type == Long.class)
/*  99 */       return (T)Long.valueOf(value); 
/* 100 */     if (type == Short.class)
/* 101 */       return (T)Short.valueOf(value); 
/* 102 */     if (type == Float.class)
/* 103 */       return (T)Float.valueOf(value); 
/* 104 */     if (type == Double.class)
/* 105 */       return (T)Double.valueOf(value); 
/* 106 */     if (type == Byte.class)
/* 107 */       return (T)Byte.valueOf(value); 
/* 108 */     if (type == Character.class) {
/* 109 */       return (T)Character.valueOf(value.charAt(0));
    }
/* 111 */     return (T)value;
  }


  private Object createRootEntity() {
    try {
/* 117 */       return this.m_entityClass.newInstance();

    }
/* 120 */     catch (Exception e) {
/* 121 */       throw new RuntimeException(String.format("Unable to create entity(%s) instance!", new Object[] { this.m_entityClass.getName() }), e);
    }
  }


/* 126 */   private boolean isTopLevel() { return (this.m_objs.size() == 1); }


  protected void onArrayBegin() {
/* 130 */     if (this.m_objs.isEmpty()) {
/* 131 */       this.m_objs.push(this.m_entities);
/* 132 */       this.m_tags.push("");
    }
  }
  protected void onArrayEnd() {
/* 136 */     this.m_objs.pop();
/* 137 */     this.m_tags.pop();
  }


/* 141 */   protected void onName(String name) { this.m_tags.push(name); }


  protected void onObjectBegin() {
/* 145 */     if (isTopLevel()) {
/* 146 */       this.m_objs.push(createRootEntity());
/* 147 */       this.m_tags.push("");
    } else {
/* 149 */       Object parent = this.m_objs.peek();
/* 150 */       String tag = (String)this.m_tags.peek();

/* 152 */       if (parent instanceof TopologyGraph) {
/* 153 */         if ("nodes".equals(tag) || "edges".equals(tag)) {
/* 154 */           this.m_objs.push(parent);
        } else {
/* 156 */           String parentTag = (this.m_tags.size() >= 2) ? (String)this.m_tags.get(this.m_tags.size() - 2) : null;

/* 158 */           if ("nodes".equals(parentTag)) {
/* 159 */             TopologyNode nodes = new TopologyNode();

/* 161 */             this.m_linker.onTopologyNode((TopologyGraph)parent, nodes);
/* 162 */             this.m_objs.push(nodes);
/* 163 */           } else if ("edges".equals(parentTag)) {
/* 164 */             TopologyEdge edges = new TopologyEdge();

/* 166 */             this.m_linker.onTopologyEdge((TopologyGraph)parent, edges);
/* 167 */             this.m_objs.push(edges);
          } else {
/* 169 */             throw new RuntimeException(String.format("Unknown tag(%s) found at %s!", new Object[] { tag, this.m_tags }));
          }
        }
      } else {
/* 173 */         throw new RuntimeException(String.format("Unknown tag(%s) found at %s!", new Object[] { tag, this.m_tags }));
      }
    }
  }

  protected void onObjectEnd() {
/* 179 */     this.m_tags.pop();

/* 181 */     Object entity = this.m_objs.pop();

/* 183 */     if (isTopLevel()) {
/* 184 */       this.m_entities.add((T)entity);
    }
  }

  protected void onValue(String value) {
/* 189 */     Object parent = this.m_objs.peek();
/* 190 */     String tag = (String)this.m_tags.pop();

/* 192 */     if (parent instanceof TopologyGraph) {
/* 193 */       parseForTopologyGraph((TopologyGraph)parent, tag, value);
/* 194 */     } else if (parent instanceof TopologyNode) {
/* 195 */       parseForTopologyNode((TopologyNode)parent, tag, value);
/* 196 */     } else if (parent instanceof TopologyEdge) {
/* 197 */       parseForTopologyEdge((TopologyEdge)parent, tag, value);
    } else {
/* 199 */       throw new RuntimeException(String.format("Unknown tag(%s) under %s!", new Object[] { tag, parent }));
    }
  }

  private void parse(JsonReader reader) throws IOException {
    try {
/* 205 */       reader.parse(this);
/* 206 */     } catch (EOFException e) {
/* 207 */       if (this.m_objs.size() > 1) {
/* 208 */         throw new EOFException(String.format("Unexpected end while parsing json! tags: %s.", new Object[] { this.m_tags }));
      }
    }

/* 212 */     this.m_linker.finish();
  }

  public void parseForTopologyEdge(TopologyEdge topologyEdge, String tag, String value) {
/* 216 */     if ("key".equals(tag)) {
/* 217 */       topologyEdge.setKey(value);
/* 218 */     } else if ("type".equals(tag)) {
/* 219 */       topologyEdge.setType(value);
/* 220 */     } else if ("target".equals(tag)) {
/* 221 */       topologyEdge.setTarget(value);
/* 222 */     } else if ("opposite".equals(tag)) {
/* 223 */       topologyEdge.setOpposite(((Boolean)convert(Boolean.class, value, Boolean.valueOf(false))).booleanValue());
/* 224 */     } else if ("weight".equals(tag)) {
/* 225 */       topologyEdge.setWeight(((Integer)convert(Integer.class, value, Integer.valueOf(0))).intValue());
/* 226 */     } else if ("status".equals(tag)) {
/* 227 */       topologyEdge.setStatus(((Integer)convert(Integer.class, value, Integer.valueOf(0))).intValue());
/* 228 */     } else if ("des".equals(tag)) {
/* 229 */       topologyEdge.setDes(value);
/* 230 */     } else if ("link".equals(tag)) {
/* 231 */       topologyEdge.setLink(value);
/* 232 */     } else if ("self".equals(tag)) {
/* 233 */       topologyEdge.setSelf(value);
/* 234 */     } else if ("dashed".equals(tag)) {
/* 235 */       topologyEdge.setDashed(((Boolean)convert(Boolean.class, value, Boolean.valueOf(false))).booleanValue());
    } else {
/* 237 */       throw new RuntimeException(String.format("Unknown tag(%s) of %s under %s!", new Object[] { tag, topologyEdge, this.m_tags }));
    }
  }

  public void parseForTopologyGraph(TopologyGraph topologyGraph, String tag, String value) {
/* 242 */     if (!"nodes".equals(tag) && !"edges".equals(tag))
    {
/* 244 */       if ("id".equals(tag)) {
/* 245 */         topologyGraph.setId(value);
/* 246 */       } else if ("type".equals(tag)) {
/* 247 */         topologyGraph.setType(value);
/* 248 */       } else if ("status".equals(tag)) {
/* 249 */         topologyGraph.setStatus(((Integer)convert(Integer.class, value, Integer.valueOf(0))).intValue());
/* 250 */       } else if ("des".equals(tag)) {
/* 251 */         topologyGraph.setDes(value);
      } else {
/* 253 */         throw new RuntimeException(String.format("Unknown tag(%s) of %s under %s!", new Object[] { tag, topologyGraph, this.m_tags }));
      }  }
  }

  public void parseForTopologyNode(TopologyNode topologyNode, String tag, String value) {
/* 258 */     if ("id".equals(tag)) {
/* 259 */       topologyNode.setId(value);
/* 260 */     } else if ("type".equals(tag)) {
/* 261 */       topologyNode.setType(value);
/* 262 */     } else if ("status".equals(tag)) {
/* 263 */       topologyNode.setStatus(((Integer)convert(Integer.class, value, Integer.valueOf(0))).intValue());
/* 264 */     } else if ("des".equals(tag)) {
/* 265 */       topologyNode.setDes(value);
/* 266 */     } else if ("link".equals(tag)) {
/* 267 */       topologyNode.setLink(value);
/* 268 */     } else if ("weight".equals(tag)) {
/* 269 */       topologyNode.setWeight(((Integer)convert(Integer.class, value, Integer.valueOf(0))).intValue());
    } else {
/* 271 */       throw new RuntimeException(String.format("Unknown tag(%s) of %s under %s!", new Object[] { tag, topologyNode, this.m_tags }));
    }
  }

  static class JsonReader { private Reader m_reader;
    private char[] m_buffer;

    public JsonReader(Reader reader) {
/* 279 */       this.m_buffer = new char[2048];






/* 286 */       this.m_reader = reader;
    }
    private int m_size; private int m_index;
    private char next() throws IOException {
/* 290 */       if (this.m_index >= this.m_size) {
/* 291 */         this.m_size = this.m_reader.read(this.m_buffer);
/* 292 */         this.m_index = 0;

/* 294 */         if (this.m_size == -1) {
/* 295 */           throw new EOFException();
        }
      }

/* 299 */       return this.m_buffer[this.m_index++];
    }

    public void parse(DefaultJsonParser parser) throws IOException {
/* 303 */       StringBuilder sb = new StringBuilder();
/* 304 */       boolean flag = false;

      while (true) {
/* 307 */         char ch = next();

/* 309 */         switch (ch) {
          case '\t':
          case '\n':
          case '\r':
          case ' ':
            continue;
          case '{':
/* 316 */             parser.onObjectBegin();
/* 317 */             flag = false;
            continue;
          case '}':
/* 320 */             if (flag) {
/* 321 */               parser.onValue(sb.toString());
/* 322 */               sb.setLength(0);
            }

/* 325 */             parser.onObjectEnd();
/* 326 */             flag = false;
            continue;
          case '"':
          case '\'':
            while (true) {
/* 331 */               char ch2 = next();

/* 333 */               if (ch2 != ch) {
/* 334 */                 if (ch2 == '\\') {
/* 335 */                   char ch3 = next();

/* 337 */                   switch (ch3) {
                    case 't':
/* 339 */                       sb.append('\t');
                      continue;
                    case 'r':
/* 342 */                       sb.append('\r');
                      continue;
                    case 'n':
/* 345 */                       sb.append('\n');
                      continue;
                  }
/* 348 */                   sb.append(ch3);

                  continue;
                }
/* 352 */                 sb.append(ch2); continue;
              }  break;
            }
/* 355 */             if (!flag) {
/* 356 */               parser.onName(sb.toString());
            } else {
/* 358 */               parser.onValue(sb.toString());
/* 359 */               flag = false;
            }

/* 362 */             sb.setLength(0);
            continue;




          case ':':
/* 369 */             if (sb.length() != 0) {
/* 370 */               parser.onName(sb.toString());
/* 371 */               sb.setLength(0);
            }

/* 374 */             flag = true;
            continue;
          case ',':
/* 377 */             if (sb.length() != 0) {
/* 378 */               if (!flag) {
/* 379 */                 parser.onName(sb.toString());
              } else {
/* 381 */                 parser.onValue(sb.toString());
              }

/* 384 */               sb.setLength(0);
            }

/* 387 */             flag = false;
            continue;
          case '[':
/* 390 */             parser.onArrayBegin();
/* 391 */             flag = false;
            continue;
          case ']':
/* 394 */             parser.onArrayEnd();
/* 395 */             flag = false;
            continue;
        }
/* 398 */         sb.append(ch);
      }
    } }

}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/classes/!/com/dianping/cat/home/dependency/graph/transform/DefaultJsonParser.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */